//
//  AppDelegate.swift
//  ZJCSina
//
//  Created by 朱建超 on 16/11/12.
//  Copyright © 2016年 itHeiMa. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    
    
    var window: UIWindow?

    //实例化自定义的collectionViewController
    lazy var selectImageView: HMSinaSelectImageController = {
        
        let flowLayout = UICollectionViewFlowLayout()
        let selectImageView = HMSinaSelectImageController(collectionViewLayout: flowLayout)
        
        flowLayout.itemSize = CGSize(width: selectImageWidth, height: selectImageWidth)
        flowLayout.minimumLineSpacing = itemJianJu
        flowLayout.minimumInteritemSpacing = itemJianJu
        flowLayout.sectionInset = UIEdgeInsets(top: itemJianJu, left: itemJianJu, bottom: 0, right: itemJianJu)
        
        return selectImageView
    }()

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        HMSQLiteTools.shared
        
        //注册通知
        registNotification()
        
        //实例化window
        window = UIWindow(frame: UIScreen.main.bounds)
        
        //设置根视图控制器
        //实例化tabbar
//        window?.rootViewController = HMSinaTabBarController()
        
        //实例化welCome控制器
//        window?.rootViewController = HMSinaWelComeController()
        
        window?.rootViewController = HMSinaUserAccountViewModel.shared.userLogin ? HMSinaWelComeController() : HMSinaTabBarController()

//        let navigationController = UINavigationController(rootViewController: selectImageView)
//        
//        window?.rootViewController = navigationController
        
        window?.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        
        //可视化window
        window?.makeKeyAndVisible()
        
        return true
    }

    //注册通知的方法
    private func registNotification() {
        
        NotificationCenter.default.addObserver(self, selector: #selector(switchRootViewController(notification:)), name: NSNotification.Name(chooseRootViewController), object: nil)
        
    }
    
    
    
    @objc private func switchRootViewController(notification : NSNotification) {
        
        //根据通知对象中的object来判断显示什么控制器
        let viewController = (notification.object == nil ? HMSinaTabBarController() : HMSinaWelComeController())
        
        window?.rootViewController = viewController
        
        //可视化时显示帧率的框架
        JPFPSStatus.sharedInstance().open()
        
    }

    //移除通知
    deinit {
        
        NotificationCenter.default.removeObserver(self)
        
    }
    
    func applicationDidEnterBackground(_ application: UIApplication) {
        
        //添加高斯模糊
        
        //清除缓存
        HMSinaStatusDAL.clearCacheStatus()
    }

}

